<?php
class home extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
		$userData = $this->session->all_userdata();
		if(!isset($userData[0])){redirect(base_url());}		
	}
	
	public function index(){
		$userData = $this->session->all_userdata();
		
		$data['form_post']	= formObjects('post');
		$data['comments']	= $this->commentModel->getAllComments();
		/*=====chat configs start=======*/
		$port = 80;
		$domain = 'http://mynode-rdmordido.dotcloud.com';
		$analyticsAccount = '';
		$analyticsDomainName = '';
		$data['config'] = array(
					'subtwich'	=>	$this->subtwich($this->input->server('HTTP_HOST')),
					'room'		=>	'lobby',
					'host'		=>	$this->input->server('SERVER_NAME'),
					'port'		=>	$port,
					'protocol'	=>	'http://',
					'node_url'	=>	$domain.':'.$port,
					'autouser'	=>	(strstr($this->input->server('QUERY_STRING'), '&autouser')) ? str_replace(" ", "_", $this->input->get('autouser')) :"",
					'username'	=>	($nick = explode(' ',$userData[0]->name)) ? $nick[0] : '' 
		);
		if (strstr($this->input->server('QUERY_STRING'), '&logout')) {
        	redirect('Location: http://' . $this->input->server('SERVER_NAME') . '/' . $this->input->get('room'));
    	}
		/*=====chat configs end=======*/
		
		$this->load->view('home',$data);
	}
	
	public function getLatestPost(){
               if(isset($this->session->userdata(0)->user_id)){
                $userid = $this->session->userdata(0)->user_id;
                if($data['latest'] = $this->commentModel->getLatest($userid)){
                    return $this->load->view('widgets/latestcomment',$data,true);
                }
            }
    }
	public function post(){

            if($this->form_validation->run('post')==true){
               $data=array(
                        'user_id' => $this->session->userdata(0)->user_id,
                        'comment' => $this->input->post('postbox')

                );
                $this->commentModel->addNewComment($data);
                $result=array(
                           'success'=>1,
                           'message'=>"post has been added",
                           'html'=> $this->getLatestPost()
                );
                echo json_encode($result);
            }
            else{
                $this->load->view('form_errors');
            }
        }
   public function removepost(){
            $id=explode('_',$this->input->post('id'));
            $commentid = $id[1];
            $this->commentModel->remove($commentid);
            echo ($this->commentModel->getCommentInfo($commentid)) ? false : $commentid;
   }
   
   	public function subtwich($_s) {
	    $host_arr = explode(".",$_s);
	    array_pop($host_arr);
	    array_pop($host_arr);
	    return implode('.',$host_arr);
	}
	
	public function chatlog(){
		$this->load->model('chatlog');
		$chatData = $this->input->post();
		$this->chatlog->newMessage($chatData);
		echo json_encode($chatData);
	}
}
